#!/bin/bash

# get_hak5_ep bash script
# author mladjo2505

# if there are no arguments then display help and exit

if [ $# == 0 ];
	then echo "Usage:	 ./get_hak5_ep.sh [ep_num] [hd | mp4 | xvid | wmv]"
	echo "Example: ./get_hak5_ep.sh 712 hd"
	exit
fi

# and if there are arguments then we go and download ;P

# first we download the html file of episode (containing the links we need)
# which is specified by command line argument. for example ./get_hak5_ep.sh 712 mp4

# we extract the file link to a variable and delete the html file so that if you (for some reason)
# kill (^C) the script it leaves only the partialy downloaded video file (so that you can continue
# your download later on [thus -c in wget]) ;P

echo "Downloading HTML with episode links..."
wget -q -c -O episode-$1.html http://www.hak5.org/$1
export linkhd=`cat episode-$1.html | grep http://www.podtrac.com/pts/redirect.mp4/bitcast-a.bitgravity.com/revision3/web/hak5/ | sed 's/^.*<p><a class="mov" href="//' | sed 's/".*$//'`
export linkhd2=`cat episode-$1.html | grep http://www.podtrac.com/pts/redirect.mp4/videos.revision3.com/revision3/web/hak5/ | sed 's/^.*<p><a class="mov" href="//' | sed 's/".*$//'`
export linkmp4=`cat episode-$1.html | grep http://www.podtrac.com/pts/redirect.mp4/bitcast-a.bitgravity.com/revision3/web/hak5/ | sed 's/^.*<a class="mov" href="//' | sed 's/".*$//'`
export linkmp4_2=`cat episode-$1.html | grep http://www.podtrac.com/pts/redirect.mp4/videos.revision3.com/revision3/web/hak5/ | sed 's/^.*a> <a class="mov" href="//' | sed 's/".*$//'`
export linkxvid=`cat episode-$1.html | grep http://www.podtrac.com/pts/redirect.avi/bitcast-a.bitgravity.com/revision3/web/hak5/ | sed 's/^.*<a class="xvid" href="//' | sed 's/".*$//'`
export linkxvid2=`cat episode-$1.html | grep http://www.podtrac.com/pts/redirect.avi/videos.revision3.com/revision3/web/hak5/ | sed 's/^.*<a class="xvid" href="//' | sed 's/".*$//'`
export linkwmv=`cat episode-$1.html | grep http://www.podtrac.com/pts/redirect.wmv/bitcast-a.bitgravity.com/revision3/web/hak5/ | sed 's/^.*<a class="wmv" href="//' | sed 's/".*$//'`
export linkwmv2=`cat episode-$1.html | grep http://www.podtrac.com/pts/redirect.wmv/videos.revision3.com/revision3/web/hak5/ | sed 's/^.*<a class="wmv" href="//' | sed 's/".*$//'`
rm episode-$1.html

	
# then we download the file specified by the video format command line argument
# hd video, mp4 video, xvid video or wmv video (btw who would ever use this option? [#fail_microsoft])

# note: we use -q in wget so it doesn't write out anything
# some progress type thing will be added some time in the future


if [ $2 == hd ];
	then echo "Downloading HD..."
	echo "Please wait this may take a while!"
	wget -q -c $linkhd $linkhd2
fi

if [ $2 == mp4 ];
	then echo "Downloading MP4..."
	echo "Please wait this may take a while!"
	wget -q -c $linkmp4 $linkmp4_2
fi

if [ $2 == xvid ];
	then echo "Downloading XVID..."
	echo "Please wait this may take a while!"
	wget -q -c $linkxvid $linkxvid2
fi

if [ $2 == wmv ];
	then echo "Downloading WMV..."
	echo "Please wait this may take a while!"
	wget -q -c $linkwmv $linkwmv2
fi

# what to add:
#  -  video file renaming option (where to save)
#  -  find a better way to extract links from html!!!
#  -  add more stuff to this list ;P
